package com.archermind.style.controller;

import com.archermind.style.bean.Scene;
import com.archermind.style.bean.User;
import com.archermind.style.interceptor.Auth;
import com.archermind.style.service.OperationService;
import com.archermind.style.service.SceneService;
import com.archermind.style.util.Result;
import com.archermind.style.util.SessionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
*@Author 丰建立
*@Date 2019/1/25 16:43
 * ["查看场景","编辑场景"]
*/

@RestController
@RequestMapping(value = "/scene")
public class SceneAction {

    @Autowired
    private SceneService sceneService;

    @Autowired
    private OperationService opService;

    @Auth("查看场景")
    @RequestMapping(value = "/sceneTree")
    public Result sceneTree() {
        List<Map> list = sceneService.getSceneTree();
       return Result.success(list);
    }

    @RequestMapping(value = "/sceneMenu")
    public Result sceneMenu() {
        List<Map> list;
        User user = SessionUtil.getCurUser();
        if(user.getId().equals(1)){
            list = sceneService.getSceneTree();
        }else {
            list = sceneService.getSceneTree(user);
        }
        return Result.success(list);
    }

    @Auth("编辑场景")
    @RequestMapping(value="/save")
    public Result save(@RequestBody Scene scene){
        try {
            sceneService.saveOrUpdate(scene);
            opService.saveLog("添加了场景"+scene.getText());
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }

    @Auth("编辑场景")
    @RequestMapping(value="/del")
    public Result del(Long sceneId){
        try {
            Scene scene = sceneService.selectById(Scene.class,sceneId);
            sceneService.delete(scene);
            opService.saveLog("删除了场景"+scene.getText());
            return Result.success();
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}